package com.kobe.game_20;

/**
 * 
 * The following iterative sequence is defined for the set of positive integers:
 * 
 * n �� n/2 (n is even) n �� 3n + 1 (n is odd)
 * 
 * Using the rule above and starting with 13, we generate the following
 * sequence:
 * 
 * 
 * 13 �� 40 �� 20 �� 10 �� 5 �� 16 �� 8 �� 4 �� 2 �� 1
 * 
 * It can be seen that this sequence (starting at 13 and finishing at 1)
 * contains 10 terms. Although it has not been proved yet (Collatz Problem), it
 * is thought that all starting numbers finish at 1.
 * 
 * Which starting number, under one million, produces the longest chain?
 * 
 * NOTE: Once the chain starts the terms are allowed to go above one million.
 * 
 */
public class Game14 {

    public static void main(String[] args) {
        // int startValue = 1;
        // int result = 0;
        // boolean found = false;
        //
        // result = startValue;
        // while (!found) {
        // if ((result - 1) % 3 == 0 && (result != 1)
        // && ((result - 1) / 3 != 1) && ((result - 1) / 3 != 3)
        // && (((result - 1) / 3) % 2 != 0)) {
        // result = (result - 1) / 3;
        // System.out.println("n �� 3n + 1 : " + result);
        // } else {
        // result = result * 2;
        // System.out.println("n �� n/2 : " + result);
        // }
        // if ((result < 1000000) && (result % 2 == 0)
        // && (result * 2 > 1000000)) {
        // found = true;
        // }
        // }
        // System.out.println(result);
        int result = 0;
        int tempValue = 0;
        int termCount = 0;
        for (int i = 16; i < 17; i++) {
            System.out.println(i);
            tempValue = i;
            while (true) {
                if (tempValue % 2 == 0) {
                    tempValue = tempValue / 2;

                    System.out.println("running: [tempValue / 2]");

                } else {
                    tempValue = 3 * tempValue + 1;

                    System.out.println("running: [3 * tempValue + 1]");

                }
                if (tempValue != 1) {
                    termCount++;
                    System.out.println("running: termCount++:  " + termCount);

                } else {
                    System.out.println("computing: " + i);
                    if (termCount > result) {
                        result = termCount;
                        System.out.println("New Count is:  " + termCount);
                    }
                    break;
                }
            }
        }

    }
}
